package slhs.tempo.TableAnalyzer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import slhs.tempo.scriptGenerator.UtilityConstants;

/**
 * Description: This class is responsible for saving and displaying the current state of the table. Current state refers
 * to the state of the table after user has modified it, for example by applying filters available on the Filter
 * Controls frame
 */

/**
 * @author Swapnil Devikar
 * @version 1.0
 */
public class SaveCurrentState implements ActionListener {
    private static final String INSERT_NEW_TAB_MESSAGE = "Specify name for new sheet";
    public static HashMap<Integer, JTable> tableNameObjectHashmap = new HashMap<Integer, JTable>();

    private final TablePanelHandler tablePanelHandlerObject;
    private JTable newTable;

    public SaveCurrentState(TablePanelHandler tablePanelHandlerObject) {
	this.tablePanelHandlerObject = tablePanelHandlerObject;

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent saveCurrentStateEvent) {

	/**
	 * Populate the fields of new table. Cannot use the original model for new table, since the tables have to be
	 * independednt
	 * */
	TableModel originalModel = ConcreteTableModelListener.getUpdatedModel();
	newTable = new JTable(originalModel.getRowCount(), originalModel.getColumnCount());

	for (int oldModelRowIndex = 0; oldModelRowIndex < originalModel.getRowCount(); oldModelRowIndex++) {
	    for (int oldModelColumnIndex = 0; oldModelColumnIndex < originalModel.getColumnCount(); oldModelColumnIndex++) {
		newTable.setValueAt(originalModel.getValueAt(oldModelRowIndex, oldModelColumnIndex), oldModelRowIndex,
			oldModelColumnIndex);
	    }
	}

	/**
	 * Set the column model for new table
	 * */
	TableColumnModel originalcolumnModel = tablePanelHandlerObject.getTable().getColumnModel();
	int columnCount = originalcolumnModel.getColumnCount();
	String[] columnIdentifiers = new String[columnCount];
	for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
	    String columnName = originalcolumnModel.getColumn(columnIndex).getIdentifier().toString();
	    columnIdentifiers[columnIndex] = columnName;
	}
	DefaultTableModel newModel = (DefaultTableModel) newTable.getModel();
	newModel.setColumnIdentifiers(columnIdentifiers);

	// Visual sugars
	newTable.setGridColor(Color.LIGHT_GRAY);
	newTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	newTable.setRowSelectionAllowed(true);
	newTable.setCellSelectionEnabled(true);
	newTable.setColumnSelectionAllowed(true);
	new TableColumnAdjuster(newTable).adjustColumns();
	JScrollPane newTableScrollPane = new JScrollPane(newTable);
	newTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	newTableScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

	String newTabName = JOptionPane.showInputDialog(INSERT_NEW_TAB_MESSAGE, "");

	if (newTabName.equals("")) {
	    newTabName = new Integer(UtilityConstants.tabCount + 1).toString();
	}
	tablePanelHandlerObject.addNewTab(newTabName, newTableScrollPane);
	tableNameObjectHashmap.put(new Integer(UtilityConstants.tabCount++), newTable);
    }
}
